﻿using CDSParser;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using DeepZoomPivotConstructor.PivotLib;
using System.Collections.Generic;
using t_TestingLibrary;
using PaperRenderingInterfaces;
using System.IO;
using System.ComponentModel.Composition;
using System.Linq;

namespace t_CDSPaperFigureExtractor
{
    
    
    /// <summary>
    ///This is a test class for CDSPaperFigureExtractorTest and is intended
    ///to contain all CDSPaperFigureExtractorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class t_CDSPaperFigureExtractor
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestInitialize]
        public void Setup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void Cleanup()
        {
            Utilities.MyClassDone();
        }

        /// <summary>
        /// Load a file instead of accessing the web.
        /// </summary>
        [Export(typeof(IWebAccess))]
        private class LoadAFile : IWebAccess
        {
            public FileInfo theFileToLoad;

            public Uri WhatWasAskedFor { get; set; }

            public string GetUriAsString(Uri whatToLoad)
            {
                WhatWasAskedFor = whatToLoad;

                if (theFileToLoad.Exists)
                {
                    using (var reader = theFileToLoad.OpenText())
                    {
                        return reader.ReadToEnd();
                    }
                }
                else
                {
                    return null;
                }
                throw new NotImplementedException();
            }


            public DateTime? GetLastModificationTime(Uri whatToCheck)
            {
                throw new NotImplementedException();
            }
        }

        [Export(typeof(IPaperRender))]
        [PaperRender("CDSFiguresFromOnePaper")]
        private class SingleCDSPaper : IPaperRender
        {
            public IEnumerable<PivotImage> Render(string renderAddress)
            {
                PivotImage pvm = new PivotImage(null);
                yield return pvm;
            }
        }

        /// <summary>
        ///A test for CDSPaperFigureExtractor Constructor
        ///</summary>
        [TestMethod()]
        public void CDSPaperFigureExtractorConstructorTest()
        {
            CDSFigureSearchList target = new CDSFigureSearchList();
            Utilities.AddPart(new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CDSParser\\searchfor24.xml") });
            Utilities.AddPart(new PaperRenderingEngine.RenderEngine());
            Utilities.AddPart(new SingleCDSPaper());
            Utilities.Compose(target);

            var result = target.Render("http://www.cnn.com");
            Assert.AreEqual(24, result.Count(), "Expected 24 papers back from that file!");
        }

        [TestMethod]
        public void TestSetsXMLFormatRequestCorrectly()
        {
            CDSFigureSearchList target = new CDSFigureSearchList();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CDSParser\\searchfor24.xml") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new PaperRenderingEngine.RenderEngine());
            Utilities.AddPart(new SingleCDSPaper());
            Utilities.Compose(target);

            var result = target.Render("http://cdsweb.cern.ch/search?cc=CMS&as=1&ln=en&m1=a&p1=CMS&f1=reportnumber&op1=n&m2=o&p2=CFT&f2=reportnumber&op2=a&m3=a&p3=&f3=&action_search=Search&c=CMS+Papers&c=&sf=&so=a&rm=&rg=10&sc=1");

            Assert.AreEqual("http://cdsweb.cern.ch/search?cc=CMS&as=1&ln=en&m1=a&p1=CMS&f1=reportnumber&op1=n&m2=o&p2=CFT&f2=reportnumber&op2=a&m3=a&p3=&f3=&action_search=Search&c=CMS+Papers&c=&sf=&so=a&rm=&rg=10&sc=1&of=xm", laf.WhatWasAskedFor.OriginalString, "URL not right");
        }

        [TestMethod]
        public void TestSetsXMLFormatRequestCorrectlyWhenAskForHTML()
        {
            CDSFigureSearchList target = new CDSFigureSearchList();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CDSParser\\searchfor24.xml") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new PaperRenderingEngine.RenderEngine());
            Utilities.AddPart(new SingleCDSPaper());
            Utilities.Compose(target);

            var result = target.Render("http://cdsweb.cern.ch/search?cc=CMS&as=1&ln=en&m1=a&p1=CMS&f1=reportnumber&op1=n&m2=o&p2=CFT&f2=reportnumber&op2=a&m3=a&p3=&f3=&action_search=Search&c=CMS+Papers&c=&sf=&so=a&rm=&rg=10&sc=1&of=hb");

            Assert.AreEqual("http://cdsweb.cern.ch/search?cc=CMS&as=1&ln=en&m1=a&p1=CMS&f1=reportnumber&op1=n&m2=o&p2=CFT&f2=reportnumber&op2=a&m3=a&p3=&f3=&action_search=Search&c=CMS+Papers&c=&sf=&so=a&rm=&rg=10&sc=1&of=xm", laf.WhatWasAskedFor.OriginalString, "URL not right");
        }

        [TestMethod]
        public void TestSetsXMLFormatRequestCorrectlyWhenAskForXML()
        {
            CDSFigureSearchList target = new CDSFigureSearchList();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CDSParser\\searchfor24.xml") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new PaperRenderingEngine.RenderEngine());
            Utilities.AddPart(new SingleCDSPaper());
            Utilities.Compose(target);

            var result = target.Render("http://cdsweb.cern.ch/search?cc=CMS&as=1&ln=en&m1=a&p1=CMS&f1=reportnumber&op1=n&m2=o&p2=CFT&f2=reportnumber&op2=a&m3=a&p3=&f3=&action_search=Search&c=CMS+Papers&c=&sf=&so=a&rm=&rg=10&sc=1&of=xm");

            Assert.AreEqual("http://cdsweb.cern.ch/search?cc=CMS&as=1&ln=en&m1=a&p1=CMS&f1=reportnumber&op1=n&m2=o&p2=CFT&f2=reportnumber&op2=a&m3=a&p3=&f3=&action_search=Search&c=CMS+Papers&c=&sf=&so=a&rm=&rg=10&sc=1&of=xm", laf.WhatWasAskedFor.OriginalString, "URL not right");
        }

        [TestMethod]
        public void TestEmptySearchReturn()
        {
            CDSFigureSearchList target = new CDSFigureSearchList();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CDSParser\\emptysearch.xml") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new PaperRenderingEngine.RenderEngine());
            Utilities.AddPart(new SingleCDSPaper());
            Utilities.Compose(target);

            var result = target.Render("http://cdsweb.cern.ch/search?cc=CMS+Physics+Analysis+Summaries&as=1&ln=en&m1=o&p1=MUON&f1=reportnumber&op1=o&m2=a&p2=&f2=reportnumber&op2=o&m3=a&p3=&f3=reportnumber&action_search=Search&c=CMS+Physics+Analysis+Summaries&c=&sf=reportnumber&so=d&rm=&rg=50&sc=1&of=xm");

            Assert.IsNotNull(result, "Was not expecting a null result for that rendering!");
            Assert.AreEqual(0, result.Count(), "Expected no images to come back!");
        }
    }
}
